using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.BLL;
using CMS.BLL.Entity;

public partial class Admin_FAQ_FAQAdd : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        MessAlert1.HideMessage();
//        Page.Title = MultipleLanguage.OWNSITE + " - " + MultipleLanguage.FAQ_DETAIL.ToLower();
        if (!IsPostBack)
        {
            BindDataToLanguageDropdown();
            BindDataToCategoryDropdown();
            SetValidate();
            InitData();
        }
    }

    private void BindDataToCategoryDropdown()
    {
        int nCurrentLang = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        DataTable dtbCategory = new DataTable();
        FAQCategory objFAQCategory = new FAQCategory();
        dtbCategory = objFAQCategory.Search();
        ddlCategory.DataSource = dtbCategory;
        ddlCategory.DataValueField = "FAQCategoryID";
        ddlCategory.DataTextField = "Description1";
        ddlCategory.DataBind();
    }

    private void BindDataToLanguageDropdown()
    {
        DataTable dtbLanguage = new DataTable();
        Languages objLanguage = new Languages();
        dtbLanguage = objLanguage.Search();
        ddlLanguage.DataSource = dtbLanguage;
        ddlLanguage.DataValueField = "LanguageID";
        ddlLanguage.DataTextField = "Description";
        ddlLanguage.DataBind();
        ddlLanguage.SelectedValue = ConvertData.ConvertToString(Support.GetDefaultLanguage()-1)  ;
    }

    private void SetValidate()
    {
        rfvTitle.ErrorMessage = MessagesValidate.FAQ_QUESTION_EMPTY[Support.GetDefaultLanguage()-1];
    }

    private void InitData()
    {
        int nCurrentLang = 0;
        string sQueery = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);
        int nSessionLangID = ConvertData.ConvertToInt(Session[Config.SESSION_LANGID]);
        try
        {
            if (sQueery == Config.ACTION_EDIT)
            {
                int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);

                if (nSessionLangID < 0)
                    nCurrentLang = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
                else
                    nCurrentLang = nSessionLangID;

                if (nItemID > 0)
                    GetData(nCurrentLang, nItemID);

                Session[Config.SESSION_LANGID] = "-1";
            }
        }
        catch
        {
            string sRedirect = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_FAQ + "&" + Config.MESS_ID + "=" + Config.ITEM_NOTEXIST;
            Response.Redirect(sRedirect);
        }
    }

    private eFAQs FAQEntity()
    {
        eFAQs entityFAQ = new eFAQs();
        int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
        string sQueery = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);
        if (nItemID > 0 && sQueery == Config.ACTION_EDIT)
        {
            FAQs objFAQ = new FAQs();
            objFAQ.LoadById(nItemID);
            entityFAQ = objFAQ.Data;
        }
        return entityFAQ;
    }

    private void GetData(int CurrentLang, int ItemID)
    {
        // Get data to view on UI Controls
        int nStatus = 0;

        FAQs objFAQ = new FAQs();
        objFAQ.LoadById(ItemID);

        int nSessionLangId = ConvertData.ConvertToInt(Session[Config.SESSION_LANGID]);
        if (nSessionLangId > 0)
        {
            ddlLanguage.SelectedValue = ConvertData.ConvertToString(nSessionLangId);
            Session[Config.SESSION_LANGID] = "0";
        }
        else
            ddlLanguage.SelectedValue = ConvertData.ConvertToString(CurrentLang);


        ddlCategory.SelectedValue = ConvertData.ConvertToString(objFAQ.Data.FAQCategoryID);
        nStatus = ConvertData.ConvertToInt(objFAQ.Data.Status);
        if (nStatus > 0)
            chkActive.Checked = true;
        else
            chkActive.Checked = false;

        switch (CurrentLang)
        {
            case 1: 
                txtTitle.Text = objFAQ.Data.Question1;
                FTBContent.IntialText(ConvertData.ConvertToString(objFAQ.Data.Answer1));
                break;
            case 2: 
                txtTitle.Text = objFAQ.Data.Question2;
                FTBContent.IntialText(ConvertData.ConvertToString(objFAQ.Data.Answer2));
                break;
            case 3: 
                txtTitle.Text = objFAQ.Data.Question3;
                FTBContent.IntialText(ConvertData.ConvertToString(objFAQ.Data.Answer3));
                break;
        }
    }

    private eFAQs SetData(int CurrentLang)
    {
        eFAQs entityFAQ = this.FAQEntity();
        FAQs objFAQ = new FAQs();
        objFAQ.Data.FAQCategoryID = ConvertData.ConvertToInt(ddlCategory.SelectedValue);
        if (chkActive.Checked) objFAQ.Data.Status = 1;
        else objFAQ.Data.Status = 0;

        string sQuestion = ConvertData.ConvertToString(ConvertData.RemoveHTMLTag(txtTitle.Text.Trim()));
        string sAnswer = ConvertData.ConvertToString(FTBContent.RenderText());

        switch (CurrentLang)
        {
            case 1:
                objFAQ.Data.Question1 = sQuestion;
                objFAQ.Data.Answer1 = sAnswer;

                objFAQ.Data.Question2 = ConvertData.ConvertToString(entityFAQ.Question2);
                objFAQ.Data.Answer2 = ConvertData.ConvertToString(entityFAQ.Answer2);

                objFAQ.Data.Question3 = ConvertData.ConvertToString(entityFAQ.Question3);
                objFAQ.Data.Answer3 = ConvertData.ConvertToString(entityFAQ.Answer3);
                break;
            case 2: 
                objFAQ.Data.Question2 = sQuestion;
                objFAQ.Data.Answer2 = sAnswer;

                objFAQ.Data.Question1 = ConvertData.ConvertToString(entityFAQ.Question1);
                objFAQ.Data.Answer1 = ConvertData.ConvertToString(entityFAQ.Answer1);

                objFAQ.Data.Question3 = ConvertData.ConvertToString(entityFAQ.Question3);
                objFAQ.Data.Answer3 = ConvertData.ConvertToString(entityFAQ.Answer3);

                break;
            case 3:
                objFAQ.Data.Question3 = sQuestion;
                objFAQ.Data.Answer3 = sAnswer;

                objFAQ.Data.Question1 = ConvertData.ConvertToString(entityFAQ.Question1);
                objFAQ.Data.Answer1 = ConvertData.ConvertToString(entityFAQ.Answer1);

                objFAQ.Data.Question2 = ConvertData.ConvertToString(entityFAQ.Question2);
                objFAQ.Data.Answer2 =ConvertData.ConvertToString( entityFAQ.Answer2);
                break;
        }

        return objFAQ.Data;
    }

    #region Click Events
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        string sGoToPageMan = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_FAQ;
        Response.Redirect(sGoToPageMan);
    }

    protected void btnSave_Click1(object sender, EventArgs e)
    {
        int nActionResult = 0;
        int nCurrentLanguage = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        int nDefaultLanguage = Support.GetDefaultLanguage();
        FAQs objFAQObj = new FAQs();
        objFAQObj.Data = SetData(nCurrentLanguage);
        string sAction = GetQuerryString(Config.ACTION);
        if (sAction == Config.ACTION_EDIT)
        {
            //Is Edit
            int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
            nActionResult = objFAQObj.Update(nItemID);
            if (nActionResult > 0)
                MessAlert1.ShowMessage(1, MessagesAlert.UPDATE_SUCCESSFUL[nDefaultLanguage-1 ]);
            else
                MessAlert1.ShowMessage(2, MessagesAlert.UPDATE_UNSUCCESSFUL[nDefaultLanguage-1 ]);
        }
        else
        {
            // Is Insert
            nActionResult = objFAQObj.Insert();
            if (nActionResult > 0)
            {
                string sURL = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_FAQ + "&" + Config.MESS_ID + "=" + Config.MESSAGE_INSERT;
                Response.Redirect(sURL);
            }
            else
            {
                FTBContent.Focus();
              
            }
        }
    }

    protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
    {
        int nCurrentLang = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        BindDataToCategoryDropdown();
        InitData();
    }
    #endregion

    #region Relative Functions
    private string GetQuerryString(string sQuerry)
    {
        string sQuerryString = "";
        sQuerryString = ConvertData.ConvertToString(Request.QueryString[sQuerry]);
        return sQuerryString;
    }
    #endregion
}
